﻿using PivotData;
using PivotIO.XML;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PivotWebsite.Controllers
{
    /// <summary>
    /// For displaying info on a collection to a web page. Mostly for debugging purposes. :-)
    /// </summary>
    public class ListController : Controller
    {
        //
        // GET: /List?cID=10

        public ActionResult Index(int cID)
        {
            Setup(cID);
            return View(this);
        }

        /// <summary>
        /// Load the required data into this view.
        /// </summary>
        /// <param name="id"></param>
        private void Setup(int id)
        {
            var c = CollectionInfoIO.LoadCollection(id);
            ID = c.Id;
            Name = c.Name;

            Images = ImageInfoIO.LoadAllImages(id);
        }

        /// <summary>
        /// The ID of the collection
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// The name of the collection
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The list of images for our view here.
        /// </summary>
        public IEnumerable<ImageInfo> Images { get; set; }

        /// <summary>
        /// Format the facet value.
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        public string FormatFacet(FacetAbsBase f)
        {
            if (f is FacetNumber)
                return (f as FacetNumber).Number.ToString();
            if (f is FacetString)
                return (f as FacetString).Value;
            if (f is FacetDateTime)
                return (f as FacetDateTime).Date.ToString();
            if (f is FacetUri)
            {
                var u = f as FacetUri;
                return string.Format("name: {0} target: {1}", u.LinkString, u.URI);
            }
            return "unkonwn facet type";
        }
    }
}
